package com.retailshop.shopuser.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.retailshop.shopuser.common.InterfaceVer;
import com.retailshop.shopuser.common.MessageConstants;
import com.retailshop.shopuser.common.ParameterUtil;
import com.retailshop.shopuser.common.Response;
import com.retailshop.shopuser.entity.OwnerUser;
import com.retailshop.shopuser.service.UserOwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "/Owner")
public class UserOwnerController {

    @Autowired
    UserOwnerService userOwnerService;

    /**
     * 读取所有数据
     *
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/List", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response List(HttpServletRequest request, @RequestBody JSONObject jsonParam) {

        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            String res = map.get("parm").toString();
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, res, request);
        } else {
            try {
                String res = map.get("parm").toString();
                int AID  = Integer.parseInt(map.get("AID").toString());
                System.out.println(AID);

                JSONObject param = JSONObject.parseObject(res);
                int page= ParameterUtil.getIntParameter(param.get("page"));
                int limit= ParameterUtil.getIntParameter(param.get("limit"));
                int id = ParameterUtil.getIntParameter(param.get("id"));
                String no = ParameterUtil.getStringParameter(param.get("no"));
                String name = ParameterUtil.getStringParameter(param.get("name"));
                String phone = ParameterUtil.getStringParameter(param.get("phone"));
                int rankServiceId = ParameterUtil.getIntParameter(param.get("rankServiceId"));
                int rankShopId = ParameterUtil.getIntParameter(param.get("rankShopId"));
                PageHelper.startPage(page, limit);
                List<OwnerUser> ownerUserList = userOwnerService.getList(id, no, name, phone,rankServiceId,rankShopId);
                if(ownerUserList!=null) {
                    PageInfo<OwnerUser> pageInfo = new PageInfo<>(ownerUserList);
                    return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), (int)pageInfo.getTotal(), "", request);
                }
                else
                {
                    return new Response(MessageConstants.MSG_SUCCESS, null, "没有数据", request);
                }
            } catch (Exception ex) {
                return new Response(MessageConstants.MSG_SYS_ERROR, ex.getMessage(), 0, "读取数据错误", request);
            }
        }
    }

    /**
     * 读取所有数据
     *
     * @param request
     * @param jsonParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/List1", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Response List1(HttpServletRequest request, @RequestBody JSONObject jsonParam) {

        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
        if (!map.get("code").toString().equals("000000")) {
            String res = map.get("parm").toString();
            return new Response(MessageConstants.MSG_ACCESS_DENY, null, res, request);
        } else {
//            try {
            String res = map.get("parm").toString();
            int AID = Integer.parseInt(map.get("AID").toString());
            System.out.println(AID);

            JSONObject param = JSONObject.parseObject(res);
            int page = ParameterUtil.getIntParameter(param.get("page"));
            int limit = ParameterUtil.getIntParameter(param.get("limit"));
            int id = ParameterUtil.getIntParameter(param.get("id"));
            String no = ParameterUtil.getStringParameter(param.get("no"));
            String name = ParameterUtil.getStringParameter(param.get("name"));
            String phone = ParameterUtil.getStringParameter(param.get("phone"));
            PageHelper.startPage(page, limit);
            List<OwnerUser> ownerUserList = userOwnerService.getList1(id, no, name, phone);
            if (ownerUserList != null) {
                PageInfo<OwnerUser> pageInfo = new PageInfo<>(ownerUserList);
                return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), (int) pageInfo.getTotal(), "", request);
            } else {
                return new Response(MessageConstants.MSG_SUCCESS, null, "没有数据", request);
            }
//            } catch (Exception ex) {
//                return new Response(MessageConstants.MSG_SYS_ERROR, ex.getMessage(), 0, "读取数据错误", request);
//            }
        }
    }

//    /**
//     * 读取所有数据
//     *
//     * @param request
//     * @param jsonParam
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value = "/HighList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
//    public Response HighList(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
//
//        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
//        if (!map.get("code").toString().equals("000000")) {
//            String res = map.get("parm").toString();
//            return new Response(MessageConstants.MSG_ACCESS_DENY, null, res, request);
//        } else {
//            try {
//                String res = map.get("parm").toString();
//                int AID  = Integer.parseInt(map.get("AID").toString());
//                System.out.println("AID:"+AID);
//                //将接收到的数据转化为对象
//                UserOwnerData userOwnerData = JSON.parseObject(res, new TypeReference<UserOwnerData>() {
//                });
//                System.out.println("完成实例化");
//                PageHelper.startPage(userOwnerData.getPage(), userOwnerData.getLimit());
//
//                //读取数据表
//                List<UserOwnerData> listUserOwnerData = userOwnerService.selectUserOwnerHighList();
//                System.out.println("完成数据读取:"+listUserOwnerData.size());
//                if (listUserOwnerData != null) {
//
//                    PageInfo<UserOwnerData> pageInfo = new PageInfo<>(listUserOwnerData);
//                    int count = (int) pageInfo.getTotal();
//                    return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), count, "", request);
//                } else {
//                    return new Response(MessageConstants.MSG_SUCCESS, null, "没有数据", request);
//                }
//            } catch (Exception ex) {
//                return new Response(MessageConstants.MSG_SYS_ERROR, ex.getMessage(), 0, "读取数据错误", request);
//            }
//        }
//    }

//    /**
//     * 读取所有数据
//     *
//     * @param request
//     * @param jsonParam
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value = "/List1", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
//    public Response List1(HttpServletRequest request, @RequestBody JSONObject jsonParam) {
//
//        Map<String, Object> map = InterfaceVer.Security(request, jsonParam);
//        if (!map.get("code").toString().equals("000000")) {
//            String res = map.get("parm").toString();
//            return new Response(MessageConstants.MSG_ACCESS_DENY, null, res, request);
//        } else {
//            try {
//                String res = map.get("parm").toString();
//                int AID  = Integer.parseInt(map.get("AID").toString());
//                System.out.println(AID);
//                //将接收到的数据转化为对象
//                UserOwnerData userOwnerData = JSON.parseObject(res, new TypeReference<UserOwnerData>() {
//                });
//
//                PageHelper.startPage(userOwnerData.getPage(), userOwnerData.getLimit());
//
//                //读取数据表
//                List<UserOwnerData> listUserOwnerData = userOwnerService.selectUserOwnerList();
//                if (listUserOwnerData != null) {
//
//                    PageInfo<UserOwnerData> pageInfo = new PageInfo<>(listUserOwnerData);
//                    int count = (int) pageInfo.getTotal();
//                    return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), count, "", request);
//                } else {
//                    return new Response(MessageConstants.MSG_SUCCESS, null, "没有数据", request);
//                }
//            } catch (Exception ex) {
//                return new Response(MessageConstants.MSG_SYS_ERROR, ex.getMessage(), 0, "读取数据错误", request);
//            }
//        }
//    }

}